#!/bin/sh
#
# Wrapper script for using a Wineskin Engine like Winehq wine bundle
#
# Copyright (C) 2018-2023 Dean M Greer
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#

cd "$(dirname "$0")"

# wine wrappers, winetricks, 7z, cabextract & unrar
export WINESKIN_APP="${PWD}"

# WINEPREFIX location
cd "../../../Contents/SharedSupport/prefix"; export WINEPREFIX="${PWD}"

# Wrappers wine PATH
cd "../wine"; export WINEPATH="${PWD}"

# Wrappes wine/bin PATH
export WINEBINPATH="${WINEPATH}/bin"

# Set $PATH
export PATH="${WINESKIN_APP}:${WINEBINPATH}:${PATH}"

# Wineskins "RUNTIME"
cd "../../Frameworks"; export WINESKIN_RUNTIME="${PWD}"

# Custom export to workaround SIP DYLD_ stripping
export WINETRICKS_FALLBACK_LIBRARY_PATH="${WINESKIN_RUNTIME}:${HOME}/Library/Application Support/Wineskin/Runtime:/opt/local/lib:${WINEPATH}/lib:${WINEPATH}/lib64:/usr/lib:/usr/libexec:/usr/lib/system:/opt/X11/lib"

# Set Wineskin default WINEDEBUG options
export WINEDEBUG="-esync,-plugplay"

# Set required MoltenVK setting
export MVK_CONFIG_FULL_IMAGE_VIEW_SWIZZLE=1   #   XXX Required by DXVK for AMD500/Intel GPUs
export MVK_ALLOW_METAL_FENCES=1               #   XXX Required by DXVK for Apple/NVidia GPUs (better FPS than CPU Emulation)
export MVK_CONFIG_RESUME_LOST_DEVICE=1        #   XXX Required by WINE (doesn't handle VK_ERROR_DEVICE_LOST correctly)

# Default directory location "should" be $HOME
cd "${HOME}"

# This section was taken from Winehq and tweaked
clear
echo "################################################################################"
echo "#                           Wine Is Not an Emulator                            #"
echo "################################################################################"
echo ""
echo " Welcome to $(wine --version)"
echo   ""
echo   " In order to start a program:"
printf "   .exe:\e[1;31m wine\e[0m\e[1;90m program.exe\e[0m\n"
printf "   .msi:\e[1;31m wine msiexec /i\e[0m\e[1;90m program.msi\e[0m\n"
echo   ""
echo   " If you want to configure wine:"
printf "  \e[1;31m winecfg\e[0m\n"
echo   ""
echo   " To get information about app compatibility:"
printf "  \e[1;31m appdb\e[0m\e[1;90m Program Name\e[0m\n"
echo   ""
if [[ ${OSTYPE:6} -ge 19 ]]; then
    exec /bin/zsh
else
    exec /bin/bash
fi
